import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 25228
 * Date: 2024-10-05
 * Time: 8:25
 */
//https://leetcode.cn/problems/copy-list-with-random-pointer/submissions/570112602/
//随机链表的复制
public class Solution1 {
    class Node {
        int val;
        Node next;
        Node random;

        public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        }
    }
    class Solution {
        public Node copyRandomList(Node head) {
            Map<Node,Node> map=new HashMap<>();
            Node cur=head;
            while (cur!=null){
                Node node=new Node(cur.val);
                map.put(cur,node);
                cur=cur.next;
            }
            cur=head;
            while (cur!=null){
                map.get(cur).next=map.get(cur.next);
                map.get(cur).random=map.get(cur.random);
                cur=cur.next;
            }
            return map.get(head);
        }
    }
}
